package com.soushin.mvpdemo.network;

import android.util.SparseArray;

import com.blankj.ALog;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.CallClazzProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.request.CustomRequest;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import retrofit2.converter.gson.GsonConverterFactory;
import com.soushin.mvpdemo.model.ResultModel;
import com.soushin.mvpdemo.model.WeatherModel;

/**
 * 接口访问
 *
 * @author SouShin
 * @time 2018/11/16 17:31
 */
public class HttpClient {
    // TODO: 2018/11/20 用于取消网络请求
    private static CustomRequest request = EasyHttp.custom()
            .addConverterFactory(GsonConverterFactory.create())//自定义的可以设置GsonConverterFactory
            .build();
    private static ApiService mService = request.create(ApiService.class);
    public static SparseArray<Disposable> disposList = new SparseArray<>();
    private static int count=0;

    /**
     * 自定义返回结果
     * 后台返回的不是正常的 code data msg
     * @param cityId
     * @param simpleCallBack
     */
    public static void getWeatherInfo(int cityId, SimpleCallBack<WeatherModel> simpleCallBack) {
        //方法一
        Disposable disposable=EasyHttp.get(cityId+".html")
                .execute(new CallBackProxy<ResultModel<WeatherModel>, WeatherModel>(simpleCallBack){});
        disposList.put(++count,disposable);
        ALog.e("添加网络请求",count);
        //方法二
        /*Observable<WeatherModel> observable = request.call(mService.getWeatherInfo(cityId));
        Disposable subscription = observable.subscribe(consumer);
        disposList.put(++count,subscription);
        //方法三
        EasyHttp.get(cityId+".html")
                .execute(new CallClazzProxy<ResultModel<WeatherModel>, WeatherModel>(WeatherModel.class) {})
                .subscribe(new BaseSubscriber<WeatherModel>() {
                    @Override
                    public void onError(ApiException e) {
                        ALog.e("打印",e.getMessage());
                    }
                    @Override
                    public void onNext(WeatherModel weatherinfoBean) {
                        super.onNext(weatherinfoBean);
                        ALog.e("打印",weatherinfoBean.toString());
                    }
                });*/
    }

    public static Observable<WeatherModel> getObservable(int cityId){
        return EasyHttp.get(cityId+".html")
                .execute(new CallClazzProxy<ResultModel<WeatherModel>, WeatherModel>(WeatherModel.class){});
    }


    /**
     * 添加订阅关系
     *
     * @param disposable
     */
    public static void addDisposable(Disposable disposable) {
        count=+1;
        ALog.e("添加Disposable", count);
        disposList.put(count, disposable);
    }

    /**
     * 在onDestroy里执行
     * 取消所有订阅的网络请求
     */
    public static void disposeRequest() {
        ALog.e("取消订阅", count);
        for (int i = 0; i < count; i++) {
            EasyHttp.cancelSubscription(disposList.get(i));
            disposList.remove(i);
        }
        count = 0;
    }

}
